/*
 *      logthread.cpp
 *
 *      Copyright 2012 David Vachulka <david@konstrukce-cad.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include <fstream>
#include "logthread.h"
#include "utils.h"
#include "dxirc.h"

#define preferences dxirc::instance()->getPreferences()

LogThread::LogThread(dxTabItem* parent, FXApp* app, FXString directory)
: m_feedback(app), m_parent(parent), m_directory(directory)
{}

FXint LogThread::run()
{
    if(m_list)
    {
        return listFiles();
    }
    return loadLogLines();
}

FXint LogThread::listFiles()
{
    m_result.clear();
    if(!FXStat::exists(m_directory))
    {
        m_feedback.message(m_parent, FXSEL(SEL_COMMAND, LogThread_LIST));
        return 0;
    }
    FXDir dir;
    FXString path, name;
    FXStat info;
    // Managed to open directory
    if(dir.open(m_directory))
    {
        // Process directory entries
        while(dir.next())
        {
            // Get name of entry
            name = dir.name();
            // A dot special file?
            if(name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) continue;
            // Hidden file or directory normally not shown
            if(name[0] == '.') continue;
            // Build full pathname of entry
            path = m_directory;
            if (!ISPATHSEP(path[path.length() - 1])) path += PATHSEPSTRING;
            path += name;
#ifndef WIN32
            // Get file/link info
            if(!FXStat::statLink(path, info)) continue;
            // If its a link, it's not dxirc logs
            if(info.isLink()) continue;
#else
            // Get file/link info
            if(!FXStat::statFile(path, info)) continue;
            // Hidden file or directory normally not shown
            if(info.isHidden()) continue;
#endif
            // If it is directory, don't need channel log haven't subfolders
            if(info.isDirectory()) continue;
            // If it is not a directory, and not showing files and matching pattern skip it
            if(!info.isDirectory() && !utils::isRightLogFile(path, name)) continue;
            else m_result.append(name);
        }
        // Close it
        dir.close();
    }
    if(!m_result.no())
    {
        m_feedback.message(m_parent, FXSEL(SEL_COMMAND, LogThread_LIST));
        return 0;
    }
    utils::sortStringArray(m_result);
    m_feedback.message(m_parent, FXSEL(SEL_COMMAND, LogThread_LIST));
    return 1;
}

FXint LogThread::loadLogLines()
{
    m_result.clear();
    if(!FXStat::exists(m_directory))
    {
        m_feedback.message(m_parent, FXSEL(SEL_COMMAND, LogThread_LINES));
        return 0;
    }
    FXDir dir;
    FXString path, name;
    FXStat info;
    dxStringArray files;
    // Managed to open directory
    if(dir.open(m_directory))
    {
        // Process directory entries
        while (dir.next())
        {
            // Get name of entry
            name = dir.name();
            // A dot special file?
            if(name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) continue;
            // Hidden file or directory normally not shown
            if(name[0] == '.') continue;
            // Build full pathname of entry
            path = m_directory;
            if (!ISPATHSEP(path[path.length() - 1])) path += PATHSEPSTRING;
            path += name;
#ifndef WIN32
            // Get file/link info
            if(!FXStat::statLink(path, info)) continue;
            // If its a link, it's not dxirc logs
            if(info.isLink()) continue;
#else
            // Get file/link info
            if(!FXStat::statFile(path, info)) continue;
            // Hidden file or directory normally not shown
            if(info.isHidden()) continue;
#endif
            // If it is directory, don't need channel log haven't subfolders
            if(info.isDirectory()) continue;
            // If it is not a directory, and not showing files and matching pattern skip it
            if(!info.isDirectory() && !utils::isRightLogFile(path, name)) continue;
            else files.append(name);
        }
        // Close it
        dir.close();
    }
    if(!files.no())
    {
        m_feedback.message(m_parent, FXSEL(SEL_COMMAND, LogThread_LINES));
        return 0;
    }
    utils::sortStringArray(files);
    dxStringArray temp;
    for(FXint i=files.no()-1; i>-1; i--)
    {
        temp.clear();
        std::ifstream file(FXString(m_directory+PATHSEPSTRING+files[i]).text());
        std::string line;
        while(getline(file, line))
            temp.append(utils::normalizeLogLine(files[i], line));
        file.close();
        FXint nlines = preferences.m_loadLines-m_result.no();
        if(nlines>0)
        {
            for(FXint j=temp.no()-1; j>-1&&nlines>0; j--, nlines--)
                m_result.prepend(temp[j]);
        }
        else break;
    }
    m_feedback.message(m_parent, FXSEL(SEL_COMMAND, LogThread_LINES));
    return 1;
}
